package com.teamcenter.rac.massupdate.ui;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.teamcenter.rac.aif.binding.SWTObservablesFactory;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.common.TCUtilities;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.massupdate.converters.Source2LabelConverter;
import com.teamcenter.rac.massupdate.model.ISubmitToWorkflowRow;
import com.teamcenter.rac.massupdate.model.MarkupChangeModel;
import com.teamcenter.rac.massupdate.model.MarkupModel;
import com.teamcenter.rac.massupdate.model.SubmitToWorkflowDialogModel;
import com.teamcenter.rac.massupdate.ui.grid.GridColumn;
import com.teamcenter.rac.massupdate.ui.grid.GridConfigurator;
import com.teamcenter.rac.massupdate.ui.grid.GridModel;
import com.teamcenter.rac.massupdate.ui.grid.IEditorConfig;
import com.teamcenter.rac.massupdate.ui.grid.MarkupChangeModelCellImagePainter;
import com.teamcenter.rac.massupdate.utils.MassUpdateHelper;
import com.teamcenter.rac.massupdate.utils.MassUpdateTableHelper;

import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GlazedLists;
import net.sourceforge.nattable.NatTable;
import net.sourceforge.nattable.config.AbstractRegistryConfiguration;
import net.sourceforge.nattable.config.CellConfigAttributes;
import net.sourceforge.nattable.config.IConfigRegistry;
import net.sourceforge.nattable.config.IEditableRule;
import net.sourceforge.nattable.data.IRowIdAccessor;
import net.sourceforge.nattable.data.convert.DefaultBooleanDisplayConverter;
import net.sourceforge.nattable.data.validate.IDataValidator;
import net.sourceforge.nattable.edit.action.ToggleCheckBoxColumnAction;
import net.sourceforge.nattable.edit.editor.CheckBoxCellEditor;
import net.sourceforge.nattable.edit.editor.ICellEditor;
import net.sourceforge.nattable.layer.DataLayer;
import net.sourceforge.nattable.painter.cell.CheckBoxPainter;
import net.sourceforge.nattable.painter.cell.ColumnHeaderCheckBoxPainter;
import net.sourceforge.nattable.painter.cell.TextPainter;
import net.sourceforge.nattable.painter.cell.decorator.BeveledBorderDecorator;
import net.sourceforge.nattable.painter.cell.decorator.CellPainterDecorator;
import net.sourceforge.nattable.style.CellStyleAttributes;
import net.sourceforge.nattable.style.HorizontalAlignmentEnum;
import net.sourceforge.nattable.style.Style;
import net.sourceforge.nattable.ui.binding.UiBindingRegistry;
import net.sourceforge.nattable.ui.matcher.CellPainterMouseEventMatcher;
import net.sourceforge.nattable.ui.util.CellEdgeEnum;
@SuppressWarnings({"rawtypes","unchecked"})
public class SubmitMarkupToWorkflowDialog extends AbstractMassUpdateDialog {
	protected MarkupModel m_markupModel;
	private Text m_markupObjectCommentText;
	private Text m_changeObjectNameText;
	private Text m_markupObjectPurposeText;
	private Button m_submitButton;
	private GridConfigurator m_tableConfigurator;
	private NatTable m_markupChangesTable;
	private final SubmitToWorkflowDialogModel m_dialogModel;
	public static final String[] PROPERTY_NAMES = { "selected", "fnd0modified_object", "fnd0context",
			"fnd0input_object", "fnd0status", "fnd0note", "release_status_list", "owning_user", "owning_group" };

	public SubmitMarkupToWorkflowDialog(Shell paramShell, MarkupModel paramMarkupModel,
			SubmitToWorkflowDialogModel<ISubmitToWorkflowRow> paramSubmitToWorkflowDialogModel) {
		super(paramShell);
		Assert.isNotNull(paramMarkupModel);
		setShellStyle(36080);
		this.m_markupModel = paramMarkupModel;
		this.m_dialogModel = paramSubmitToWorkflowDialogModel;
	}

	protected void createButtonsForButtonBar(Composite paramComposite) {
		this.m_submitButton = createButton(paramComposite, 1090, IMassUpdateDialogConstants.SUBMIT_LABEL, false);
		this.m_submitButton.setToolTipText(Messages.SubmitToWorkFlowDialog_submitButton_toolTipText);
		this.m_submitButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent paramAnonymousSelectionEvent) {
				SubmitMarkupToWorkflowDialog.this.submitToWorkflow();
			}
		});
		this.m_submitButton.setEnabled(false);
		Button localButton = createButton(paramComposite, 12, IDialogConstants.CLOSE_LABEL, false);
		localButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent paramAnonymousSelectionEvent) {
				SubmitMarkupToWorkflowDialog.this.close();
			}
		});
		localButton.setFocus();
		this.m_bindingContext = initDataBindings();
		setupAggregateValidationStatus();
		initCommonDataBindings(this.m_bindingContext);
	}

	protected Control createContents(Composite paramComposite) {
		Control localControl = super.createContents(paramComposite);
		setTitle(Messages.SubmitToWorkflowDialog_Title);
		getShell().setText(Messages.SubmitToWorkflowDialog_Title);
		setMessage("");
		return localControl;
	}

	protected Control createDialogArea(Composite paramComposite) {
		Composite localComposite1 = (Composite) super.createDialogArea(paramComposite);
		GridLayout localGridLayout = new GridLayout(1, false);
		localGridLayout.verticalSpacing = 0;
		localGridLayout.marginWidth = 0;
		localGridLayout.marginHeight = 0;
		localGridLayout.horizontalSpacing = 0;
		Composite localComposite2 = new Composite(localComposite1, 0);
		localComposite2.setLayoutData(new GridData(4, 4, true, true, 1, 1));
		localComposite2.setLayout(localGridLayout);
		contributeChangeInformationGroup(localComposite2);
		contributeChangeTableGroup(localComposite2);
		return localComposite1;
	}

	protected void initCommonDataBindings(DataBindingContext paramDataBindingContext) {
	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext localDataBindingContext = new DataBindingContext();
		ISWTObservableValue localISWTObservableValue1 = SWTObservables.observeText(this.m_changeObjectNameText, 24);
		IObservableValue localIObservableValue1 = BeansObservables.observeValue(this.m_markupModel,
				"changeItemRevision");
		UpdateValueStrategy localUpdateValueStrategy = new UpdateValueStrategy();
		localUpdateValueStrategy.setConverter(new Source2LabelConverter());
		localDataBindingContext.bindValue(localISWTObservableValue1, localIObservableValue1,
				new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), localUpdateValueStrategy);
		ISWTObservableValue localISWTObservableValue2 = SWTObservables.observeText(this.m_markupObjectPurposeText, 24);
		IObservableValue localIObservableValue2 = BeansObservables.observeValue(this.m_markupModel, "fnd0purpose");
		localDataBindingContext.bindValue(localISWTObservableValue2, localIObservableValue2,
				new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), null);
		ISWTObservableValue localISWTObservableValue3 = SWTObservables.observeText(this.m_markupObjectCommentText, 24);
		IObservableValue localIObservableValue3 = BeansObservables.observeValue(this.m_markupModel, "fnd0comment");
		localDataBindingContext.bindValue(localISWTObservableValue3, localIObservableValue3,
				new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), null);
		IObservableValue localIObservableValue4 = SWTObservablesFactory.getInstance()
				.observeEnabled(this.m_submitButton);
		IObservableValue localIObservableValue5 = BeansObservables.observeValue(this.m_dialogModel,
				"somethingSelected");
		localDataBindingContext.bindValue(localIObservableValue4, localIObservableValue5,
				new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), null);
		return localDataBindingContext;
	}

	private GridColumn[] contributeTableColumns() {
		String[] arrayOfString1 = { "selected", "Fnd0MarkupChange.fnd0modified_object", "Fnd0MarkupChange.fnd0context",
				"Fnd0MarkupChange.fnd0input_object", "Fnd0MarkupChange.fnd0status", "Fnd0MarkupChange.fnd0note",
				"release_status_list", "owning_user", "owning_group" };
		int i = PROPERTY_NAMES.length;
		int[] arrayOfInt = { 75, 150, 120, 120, 120, 120, 110, 110, 90 };
		GridColumn[] arrayOfGridColumn = new GridColumn[i];
		String[] arrayOfString2 = TCUtilities.getLocalizedValues(arrayOfString1);
		int j = MassUpdateTableHelper.getIndexByValue(PROPERTY_NAMES, "selected");
		arrayOfString2[j] = "";
		for (int k = 0; k < i; k++) {
			GridColumn localGridColumn = new GridColumn(k, PROPERTY_NAMES[k], arrayOfString2[k]);
			localGridColumn.setWidth(Integer.valueOf(arrayOfInt[k]));
			arrayOfGridColumn[k] = localGridColumn;
		}
		arrayOfGridColumn[j].setEditable(true);
		arrayOfGridColumn[j].setEditorConfig(new IEditorConfig() {
			public IEditorConfig.EditorType getEditorType() {
				return IEditorConfig.EditorType.CHECKBOX;
			}

			public IDataValidator getValidator() {
				return null;
			}

			public IEditableRule getEditableRule() {
				return IEditableRule.ALWAYS_EDITABLE;
			}

			public ICellEditor getCellEditor() {
				return new CheckBoxCellEditor();
			}
		});
		arrayOfGridColumn[j].setDisplayConverter(new DefaultBooleanDisplayConverter());
		arrayOfGridColumn[j].setCellPainter(new CheckBoxPainter());
		Style localStyle = new Style();
		localStyle.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT, HorizontalAlignmentEnum.CENTER);
		arrayOfGridColumn[j].setColumnStyle(localStyle);
		arrayOfGridColumn[j].setColumnHeaderStyle(localStyle);
		int m = MassUpdateTableHelper.getIndexByValue(PROPERTY_NAMES, "fnd0modified_object");
		arrayOfGridColumn[m].setCellPainter(new CellPainterDecorator(new TextPainter(), CellEdgeEnum.LEFT,
				new MarkupChangeModelCellImagePainter("fnd0modified_object", "object_string", "")));
		int n = MassUpdateTableHelper.getIndexByValue(PROPERTY_NAMES, "fnd0context");
		arrayOfGridColumn[n].setCellPainter(new CellPainterDecorator(new TextPainter(), CellEdgeEnum.LEFT,
				new MarkupChangeModelCellImagePainter("fnd0context", "object_string", "")));
		int i1 = MassUpdateTableHelper.getIndexByValue(PROPERTY_NAMES, "fnd0input_object");
		arrayOfGridColumn[i1].setCellPainter(new CellPainterDecorator(new TextPainter(), CellEdgeEnum.LEFT,
				new MarkupChangeModelCellImagePainter("fnd0input_object", "object_string", "")));
		int i2 = MassUpdateTableHelper.getIndexByValue(PROPERTY_NAMES, "owning_user");
		arrayOfGridColumn[i2].setCellPainter(new CellPainterDecorator(new TextPainter(), CellEdgeEnum.LEFT,
				new MarkupChangeModelCellImagePainter("fnd0modified_object", "owning_user", "")));
		int i3 = MassUpdateTableHelper.getIndexByValue(PROPERTY_NAMES, "owning_group");
		arrayOfGridColumn[i3].setCellPainter(new CellPainterDecorator(new TextPainter(), CellEdgeEnum.LEFT,
				new MarkupChangeModelCellImagePainter("fnd0modified_object", "owning_group", "")));
		int i4 = MassUpdateTableHelper.getIndexByValue(PROPERTY_NAMES, "release_status_list");
		arrayOfGridColumn[i4].setCellPainter(new CellPainterDecorator(new TextPainter(), CellEdgeEnum.RIGHT,
				new MarkupChangeModelCellImagePainter("fnd0modified_object", "release_status_list", "")));
		return arrayOfGridColumn;
	}

	private void contributeChangeInformationGroup(Composite paramComposite) {
		GridLayout localGridLayout1 = new GridLayout(1, false);
		localGridLayout1.verticalSpacing = 0;
		localGridLayout1.marginWidth = 0;
		localGridLayout1.marginHeight = 0;
		localGridLayout1.horizontalSpacing = 0;
		Group localGroup = new Group(paramComposite, 0);
		localGroup.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
		localGroup.setLayout(localGridLayout1);
		localGroup.setText(Messages.ChangeMarkupDialog_Grp_ChangeInformation);
		GridLayout localGridLayout2 = new GridLayout(2, false);
		localGridLayout2.marginLeft = 10;
		Composite localComposite = new Composite(localGroup, 0);
		localComposite.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
		localComposite.setLayout(localGridLayout2);
		Label localLabel1 = new Label(localComposite, 0);
		FontData localFontData1 = localLabel1.getFont().getFontData()[0];
		localLabel1.setFont(JFaceResources.getFontRegistry().getBold(localFontData1.getName()));
		localLabel1.setLayoutData(new GridData(131072, 16777216, false, false, 1, 1));
		localLabel1.setText(Messages.SubmitMarkupToWorkflowDialog_Lbl_ChangeObject);
		this.m_changeObjectNameText = new Text(localComposite, 8);
		this.m_changeObjectNameText.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
		Label localLabel2 = new Label(localComposite, 0);
		FontData localFontData2 = localLabel2.getFont().getFontData()[0];
		localLabel2.setFont(JFaceResources.getFontRegistry().getBold(localFontData2.getName()));
		localLabel2.setText(Messages.ChangeMarkupDialog_Lbl_Purpose);
		this.m_markupObjectPurposeText = new Text(localComposite, 8);
		this.m_markupObjectPurposeText.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
		Label localLabel3 = new Label(localComposite, 0);
		FontData localFontData3 = localLabel3.getFont().getFontData()[0];
		localLabel3.setFont(JFaceResources.getFontRegistry().getBold(localFontData3.getName()));
		localLabel3.setText(Messages.ChangeMarkupDialog_Lbl_Comment);
		this.m_markupObjectCommentText = new Text(localComposite, 8);
		this.m_markupObjectCommentText.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
	}

	protected void contributeChangeTableGroup(Composite paramComposite) {
		Group localGroup = new Group(paramComposite, 0);
		localGroup.setLayoutData(new GridData(4, 4, true, true, 1, 1));
		localGroup.setText(Messages.MassUpdateDialog_Grp_OperationalData);
		GridLayout localGridLayout1 = new GridLayout(1, false);
		localGridLayout1.verticalSpacing = 0;
		localGridLayout1.marginWidth = 0;
		localGridLayout1.marginHeight = 0;
		localGridLayout1.horizontalSpacing = 0;
		localGroup.setLayout(localGridLayout1);
		Composite localComposite = new Composite(localGroup, 0);
		GridData localGridData = new GridData(4, 4, true, true, 1, 1);
		localGridData.verticalIndent = 5;
		localComposite.setLayoutData(localGridData);
		GridLayout localGridLayout2 = new GridLayout(1, false);
		localGridLayout2.verticalSpacing = 0;
		localGridLayout2.marginLeft = 10;
		localGridLayout2.marginWidth = 0;
		localGridLayout2.marginHeight = 0;
		localGridLayout2.horizontalSpacing = 0;
		localComposite.setLayout(localGridLayout2);
		GridColumn[] arrayOfGridColumn = contributeTableColumns();
		GridModel localGridModel = new GridModel(arrayOfGridColumn);
		IRowIdAccessor local4 = new IRowIdAccessor<MarkupChangeModel>() {
			@Override
			public Serializable getRowId(MarkupChangeModel paramAnonymousMarkupChangeModel) {
				return (Serializable) paramAnonymousMarkupChangeModel.getMarkupChange().getPropertyValue("UID");
			}
		};
		EventList localEventList = GlazedLists.eventList(new ArrayList());
		localEventList.addAll(this.m_dialogModel.getRows());
		this.m_tableConfigurator = new GridConfigurator(localComposite, localGridModel, localEventList, local4);
		this.m_markupChangesTable = this.m_tableConfigurator.setupLayers();
		this.m_markupChangesTable.addConfiguration(getSelectAllColHeaderConfiguration());
		this.m_markupChangesTable.configure();
		this.m_markupChangesTable.setLayoutData(new GridData(4, 4, true, true, 1, 1));
		this.m_markupModel.setMarkupChanges(this.m_tableConfigurator.getEventList());
		this.m_markupChangesTable.setLayoutData(new GridData(4, 4, true, true, 1, 1));
	}

	private void submitToWorkflow() {
		List localList = this.m_dialogModel.getSelectedComponents();
		MassUpdateHelper.submitTargetsToWorkflow(
				(InterfaceAIFComponent[]) localList.toArray(new TCComponent[localList.size()]));
	}

	private AbstractRegistryConfiguration getSelectAllColHeaderConfiguration() {
		final DataLayer localDataLayer = this.m_tableConfigurator.getBodyLayerStack().getDataLayer();
		final ColumnHeaderCheckBoxPainter localColumnHeaderCheckBoxPainter = new ColumnHeaderCheckBoxPainter(
				localDataLayer);
		final BeveledBorderDecorator localBeveledBorderDecorator = new BeveledBorderDecorator(
				localColumnHeaderCheckBoxPainter);
		return new AbstractRegistryConfiguration() {
			public void configureRegistry(IConfigRegistry paramAnonymousIConfigRegistry) {
				String str = "colHdrLbl_" + MassUpdateTableHelper
						.getIndexByValue(SubmitMarkupToWorkflowDialog.PROPERTY_NAMES, "selected");
				paramAnonymousIConfigRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER,
						localBeveledBorderDecorator, "NORMAL", str);
			}

			public void configureUiBindings(UiBindingRegistry paramAnonymousUiBindingRegistry) {
				paramAnonymousUiBindingRegistry.registerFirstSingleClickBinding(
						new CellPainterMouseEventMatcher("COLUMN_HEADER", 1, localColumnHeaderCheckBoxPainter),
						new ToggleCheckBoxColumnAction(localColumnHeaderCheckBoxPainter, localDataLayer));
			}
		};
	}
}
